package robuin.ioio.config;

//import robuin.ioio.config.IOIOSeek.SlowAdapter;

import java.util.ArrayList;
import java.util.Arrays;

import robuin.ioio.config.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Spinner;
import android.widget.Toast;

	public class uartConfig extends Activity{
		private ArrayList<Integer> txPins = new ArrayList<Integer>(
				Arrays.asList(3,4,5,6,7,10,11,12,13,14,27,28,29,30,31,32));
		private ArrayList<Integer> rxPins = new ArrayList<Integer>(
				Arrays.asList(3,4,5,6,7,9,10,11,12,13,14,27,28,29,30,31,32));
		private ArrayList<String> baudRates = new ArrayList<String>(
				Arrays.asList("9600","38400","19200"));
	    private TextView mStatus;
	    private Spinner spinnerRx;
	    private Spinner spinnerTx;
	    private Spinner spinnerBaudRate;
	    
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	        Intent intent = getIntent();
	        String message = intent.getStringExtra(IOIOConfig.EXTRA_MESSAGE);
	        
	        setContentView(R.layout.uartsetup);

	        mStatus = (TextView) findViewById(R.id.status1);
	        mStatus.setText(message);
	        
	        initializeSpinners();
	    }

		private void initializeSpinners() {
			spinnerTx = (Spinner)findViewById(R.id.Spinner01);
			spinnerRx = (Spinner)findViewById(R.id.Spinner02);
			spinnerBaudRate = (Spinner)findViewById(R.id.Spinner03);
			
			ArrayAdapter<Integer> integerDataAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, txPins);
			integerDataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinnerTx.setAdapter(integerDataAdapter);
			
			integerDataAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, rxPins);
			integerDataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinnerRx.setAdapter(integerDataAdapter);
			
			ArrayAdapter<String> stringDataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, baudRates);
			stringDataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinnerBaudRate.setAdapter(stringDataAdapter);
		}
		
		public void buttonOnClick(View v) {
			String txPin = spinnerTx.getSelectedItem().toString();
			String rxPin = spinnerRx.getSelectedItem().toString();
			String baudRate = spinnerBaudRate.getSelectedItem().toString();
			
		    if(txPin == rxPin) {
		    	Toast errorMsg = Toast.makeText(this, " Tx and Rx pins should be different.", Toast.LENGTH_SHORT);
		    	errorMsg.setGravity(Gravity.AXIS_SPECIFIED, 0, 0);
		    	errorMsg.show();
		    	return;
		    }
		    
		    Intent i = new Intent(this, uartConnect.class);
		    i.putExtra("TX_PIN", txPin);
		    i.putExtra("RX_PIN", rxPin);
		    i.putExtra("BAUD_RATE", baudRate);
		    startActivity(i);
		    
		}
	}